package com.lpfy.dtxc.utils;

import android.os.Environment;
import android.util.Log;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author: hanGuangHui
 * @data: 2024/2/22
 * @description:
 */
public class DownloadFileUtil {

    public void startDownload(String netUrl){
        Log.e("下载状态","开始下载");
        try {
            URL url = new URL(netUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            int fileSize = connection.getContentLength();
            InputStream inputStream = connection.getInputStream();
            FileOutputStream outputStream = new FileOutputStream(Environment.getExternalStorageDirectory() + "/file2.pdf");
            byte[] buffer = new byte[4096];
            int bytesRead;
            int totalBytesRead = 0;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
                totalBytesRead += bytesRead;
                int progress = (int) (totalBytesRead * 100 / fileSize);
                Log.e("下载进度",progress+"===");
//                publishProgress(progress);
            }
            Log.i("下载进度", "download ok");
            outputStream.close();
            inputStream.close();
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
